package com._4pd.demo.test;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.EnvVar;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.dsl.FilterWatchListDeletable;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.PrettyLoggable;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Main {
    public static void main(String[] args) throws Exception {
        Main main = new Main();
        String[] res = new String[20];
//        main.getPodList("");
        String argoLabel = "od-simulate-workflow-cz8l8";
        String path = "/Users/yu/test-time/simulate/simulate/nofilter/simulate3";

        KubernetesClient client = new KubernetesClientBuilder().build();
        FilterWatchListDeletable<Pod, PodList, PodResource> pods = client.pods().inNamespace("airflow").withLabel("workflows.argoproj.io/workflow", argoLabel);
        for (Pod pod : pods.list().getItems()) {
            String name = pod.getMetadata().getName();
            if (name.contains("od-simulate-simulator")){
                // 模拟器
                List<Container> containers = pod.getSpec().getContainers();
                for (Container container : containers) {
                    if (Objects.equals(container.getName(), "main")) {
                        String index = null;
                        for (EnvVar envVar : container.getEnv()) {
                            if (Objects.equals(envVar.getName(), "SIMULATOR_INDEX")) {
                                index = envVar.getValue();
                                break;
                            }
                        }
                        String loggable = client.pods().inNamespace("airflow").withName(name).inContainer(container.getName()).tailingLines(15).getLog();
                        String[] split = loggable.split("\n");
                        int[] d = new int[4];
                        int index_value = 0;
                            for (String line: split){
                                try {
                                    int value = Integer.parseInt(line);
                                    d[index_value] = value;
                                    index_value++;
                                }catch (Exception e){

                                }

                            }
                        System.out.println(("k logs -n airflow "+name+" > "+path+"/simulate-container"+Integer.parseInt(index)+".log"));
                        res[Integer.parseInt(index)] = String.format("%s+%s+%s\n=%s", d[0], d[1], d[2], d[3]);
                        break;
                    }
                }
            } else if (name.contains("od-demand-export-data")) {
                // 导出数据
                System.out.println();
//                String loggable = client.pods().inNamespace("airflow").withName(name).tailingLines(50).getLog();

            } else if (name.contains("od-demand-export-data")) {
                // 合并结果
            }else{

            }


        }
        for (int i = 0; i < res.length; i++) {
            System.out.println(i);
            System.out.println(res[i]);
        }
        client.close();
    }

    public List<String> getPodList(String name) throws Exception {
        String shell = "k get pod -n airflow | grep " + name;
        shell = "k get pod -A";
        System.out.println(shell);
        Process exec = Runtime.getRuntime().exec(shell);
        exec.waitFor();
        BufferedReader bufferedInputStream = new BufferedReader(new InputStreamReader(exec.getInputStream()));
        String s = bufferedInputStream.readLine();
        System.out.println(s);
        return null;
    }

}
